﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Examples
{
    /// <summary>
    /// Prints all folders and files in document library in hierarchical order
    /// </summary>
    class PrintDocumentLibraryHierarchy
    {
        public static void DoIt(string connectionString)
        {
            using (var connection = new Camelot.SharePointConnector.Data.SharePointConnection(connectionString))
            {
                connection.Open();

                var data = Camelot.SharePointIntegration.DocumentLibraryDataUtility.CreateDocumentLibraryData("Shared Documents", null, connection);

                PrintLevel(data.Documents, 0);
            }
        }

        private static void PrintLevel(Camelot.SharePointIntegration.Client.DocumentLibraryNodeCollection documents, int level)
        {
            foreach (var node in documents.Where(n => (n.Level == level || level == -1) && n.ContentType == "Folder"))
            {
                Console.WriteLine(string.Empty.PadLeft(node.Level * 5) + "[+]" + node.Name);
                if (node.HasChildren)
                    PrintLevel(node.GetChildren() as Camelot.SharePointIntegration.Client.DocumentLibraryNodeCollection, -1);
            }

            foreach (var node in documents.Where(n => (n.Level == level || level == -1) && n.ContentType != "Folder"))
            {
                Console.WriteLine(string.Empty.PadLeft(node.Level * 5) + node.Name + " (" + ((double)node.FileSize / 1024).ToString("N2") + "kB)");
            }
        }
    }
}
